package com.mcb_wxc.service;

import com.mcb_wxc.entity.TAgent;
import com.mcb_wxc.entity.TRongyun;
import com.mcb_wxc.mapper.TAgentMapper;
import com.mcb_wxc.mapper.TRongyunMapper;
import com.mcb_wxc.util.CodeUtil;
import com.mcb_wxc.util.ReturnJsonResultUtil;
import com.mcb_wxc.util.StringUtil;
import com.mcb_wxc.util.rongyun.RongYunUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by 小生
 * 2019/4/1 15:56.
 * 即时通讯
 */
@Api(tags = "rongYun", description = "即时通讯")
@RestController
@RequestMapping(value="/rongYunService")
public class RongYunService {
    @Autowired
    TAgentMapper tAgentMapper;
    @Autowired
    TRongyunMapper tRongyunMapper;


    @ApiOperation(value="registerUser", notes="注册用户" , httpMethod = "POST", response = UserService.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "id",required = false,dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "userName",value = "用户昵称",required = false,dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "portrait",value = "用户头像",required = false,dataType = "String",paramType="query"),
    })
    @RequestMapping(value = "/registerUser" , method = RequestMethod.POST)
    @Transactional
    public String registerUser(String id, String userName, String portrait){
        ReturnJsonResultUtil result = new ReturnJsonResultUtil();
        String token = null;
        try {
            if(StringUtil.empty(userName) || StringUtil.empty(id) || StringUtil.empty(portrait)) {
                result.setContent("参数错误！");
                result.setResultCode(CodeUtil.FAIL);
                return result.toString();
            }
            TAgent tAgent = tAgentMapper.selectByPrimaryKey(Integer.parseInt(id));
            if(tAgent == null) {
                result.setContent("id无效！");
                result.setResultCode(CodeUtil.FAIL);
                return result.toString();
            }
            TRongyun tRongyun = new TRongyun();
            tRongyun.setAgentId(tAgent.getAgentId());
            tRongyun = tRongyunMapper.selectOne(tRongyun);
            if(tRongyun == null) {
                tRongyun = new TRongyun();
                token = RongYunUtil.getToken(id,userName,portrait);
                tRongyun.setAgentId(tAgent.getAgentId());
                tRongyun.setImToken(token);
                tRongyunMapper.insert(tRongyun);
            }else {
                token = tRongyun.getImToken();
            }
            result.setMainData(token);
        }catch (Exception e){
            System.out.println("注册rongCloud账号出错" + e.getMessage());
            //抛出运行时异常事务才能回滚
            throw new RuntimeException();
        }
        return result.toString();
    }

    public static void main(String[] args) {
    }
}
